package com.csw.mysqldate.jdbctemplete;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/testjdbc")
public class TestJdbcTemplete {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/select")
    public void testselect() {
        int count = 0;
        while (true) {
            count++;
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user ");
            for (int i = 0; i < maps.size(); i++) {
                Map map = maps.get(i);
            }
            if (count == Math.pow(10, 6)) {
                break;
            }
            //count+"");
        }
    }

    @RequestMapping("/insert")
    public void testinsert() {
        int count = 0;
        while (true) {
            count++;
            String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            jdbcTemplate.execute("insert into people values (null,'" + date + "','" + date + "')");

            //count+"");
            if (count == Math.pow(10, 6)) {
                break;
            }
        }
    }

    /**
     * jdbc 批量异步插入
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/jdbcbatchTest")
    public void jdbcbatchTest() throws Exception {
        String sql="insert into xxx(id,name)values(?,?)";
        //获取数据
        List<Map> dataList = new ArrayList();

        //数据格式转换
        List<Object[]> changeList = new ArrayList();
        for (int i = 0; i < dataList.size(); i++) {
            Map map= (Map) dataList.get(i);
            //数据遍历塞入相应的值
            changeList.add(new Object[]{map.get("1"),map.get("2")});
        }
        //按照500条数据分组分组
        List<List<Object[]>> blList= Lists.partition(changeList, 500);
        //每次批量执行一个组的数据，并且用异步的形式
        List<CompletableFuture> futureList = new ArrayList<>();
        for (int i = 0; i < blList.size(); i++) {
            List<Object[]> zXList = blList.get(i);
            CompletableFuture<Object> future=CompletableFuture.supplyAsync(()->{
                int[] ints = jdbcTemplate.batchUpdate(sql, zXList);
                return ints[0];
            });
            futureList.add(future);
        }

        //等待所有异步线程执行完
        for (int i = 0; i < futureList.size(); i++) {
            CompletableFuture future = futureList.get(i);
            int o = (int) future.get();
        }

    }

    /*
     *
     *
     * curl --location --request POST 'http://localhost:8080/test/jdbc/select'
     * */
}
